import { RuleProp } from '../../hooks/inputValidator'
const emilReg = /^[A-Za-zd0-9]+([-_.][A-Za-zd]+)*@([A-Za-zd]+[-.])+[A-Za-zd]{2,5}$/
interface ValidatorMapProp{
  [key: string]: RuleProp;
}
const ValidatorMap: ValidatorMapProp = {
  required: {
    validator: (value: string | object) => {
      if (typeof value === 'string') {
        return !!value.trim()
      } else {
        return false
      }
    },
    message: '请输入内容'
  },
  email: {
    validator: (value: string | object) => {
      if (typeof value === 'string') {
        return emilReg.test(value as string)
      } else {
        return false
      }
    },
    message: '请输入正确的电子邮箱'
  }
}

/**
 * 获取指定的校验器
 * 后期包括临时注册的校验器
 * @param keyName 公用校验器
 */
const getValidator = (keyName: string) => {
  return ValidatorMap[keyName]
}
export default getValidator
